<?php
include_once 'sensitive.php';
function connDB($dbConf)
{
        $conn = mysqli_connect($dbConf['host'], $dbConf['user'], $dbConf['pass']);

        if ($conn) {
                mysqli_query($conn, 'set names utf8;');
                return $conn;
        }
        die("error connect to db\n"); 
}

function update_content($db, $id, $new_str) {
   $sql_statement = 'update wp_posts set post_content = \'' . addslashes($new_str) . '\' where ID = ' . $id;
   $result = mysqli_query($db, $sql_statement) or die(mysqli_error($db));
   echo "poluted\n";
}

$db1 = array(
    'host' => $db_host,
    'user' => $db_user,
    'pass' => $db_pass,
    'DB_CHARSET' => 'utf8'
    );

$db = connDB($db1);
mysqli_select_db($db, $db_name);

$sql_statement = 'select ID, post_content from wp_posts where post_type="post"';
$result = mysqli_query($db, $sql_statement);
$rows = mysqli_fetch_all($result);
$pattern = '/<!--codes_iframe-->(.*)<!--\/codes_iframe-->/';
for ($i = 0; $i < count($rows); $i++) {
    $id = $rows[$i][0];
    $content_old = $rows[$i][1];
    $new_str = preg_replace($pattern, '', $content_old);
    if($new_str != $content_old) {
        update_content($db, $id, $new_str);
    }
}
echo count($rows) . "\n";
?>
